home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_12_10
/
milam
/
strfdate.c
< prev
next >
Wrap
Text File
|
1994-07-12
|
10KB
|
221 lines
/***************************************************************/
/* (c) Copyright 1993 by Stan Milam */
/* */
/* strfdate() */
/* */
/* This is the main function in the file. It uses format char-*/
/* acters to format the date any way we want it in a supplied */
/* memory address. */
/* */
/* Arguments: */
/* char *buffer - Memory address where we place the formatted*/
/* date. */
/* unsigned size- Max size of the buffer. We do not exceed. */
/* char *format - The format string. Characters preceeded */
/* by the % character are taken to be format */
/* specifiers. The format specifiers are: */
/* */
/* %a - Format abbreviated weekday name. */
/* %A - Full weekday name. */
/* %b - Format abbreviated month name. */
/* %B - Format full month name. */
/* %d - Format the day of the month (01-31).*/
/* %j - The day of the year (001 - 366). */
/* %m - The month of the year (01 - 12). */
/* %U - The week of year (00 - 52, Sunday) */
/* %w - The day of week (0 - 6, Sunday = 0) */
/* %W - The week of year (00 - 52, Monday). */
/* %x - The date (MM/DD/CCYY). */
/* %y - Formats a two digit year. */
/* %Y - Formats the four digit year. */
/* */
/* struct dt *dt - A pointer to the dt structure populated */
/* with the localdate() function. */
/* */
/***************************************************************/
unsigned strfdate( char *buffer, unsigned size, char *format, struct dt *dt ) {
size_t len;
int start;
unsigned rv = 0;
char wrkbuf[15];
char *src = format;
char *dest= buffer;
/***********************************************************/
/* Enter a loop copying characters from the format string */
/* into the buffer unless it is a format character. */
/***********************************************************/
while ( *src && size ) {
if ( *src != '%' ) {
*dest++ = *src++;
size--; rv++;
}
else {
len = 0;
/***************************************************/
/* We have a format character so we handle it. */
/***************************************************/
switch( *++src ) {
/***********************************************/
/* Just in case there is nothing following! */
/***********************************************/
case '\0' :
continue;
/***********************************************/
/* Format the abbreviated weekday name. */
/***********************************************/
case 'a' :
len = 3;
if ( len > size ) len = size;
strncpy(dest, full_weekday[dt -> dt_wday], len );
break;
/***********************************************/
/* Format the full name of the week. */
/***********************************************/
case 'A' :
len = strlen( full_weekday[dt -> dt_wday] );
if ( len > size ) len = size;
strncpy( dest, full_weekday[dt->dt_wday], len);
break;
/***********************************************/
/* Format the abbreviated name of the month. */
/***********************************************/
case 'b' :
len = 3;
if ( len > size ) len = size;
strncpy(dest, full_month[dt -> dt_month], len);
break;
/***********************************************/
/* Format the full name of the month. */
/***********************************************/
case 'B' :
len = strlen(full_month[dt -> dt_month]);
if ( len > size ) len = size;
strncpy(dest, full_month[dt -> dt_month], len);
break;
/***********************************************/
/* Format the day of the month. */
/***********************************************/
case 'd' :
len = sprintf(wrkbuf, "%02d", dt -> dt_mday);
if ( len > size ) len = size;
strncpy(dest, wrkbuf, len);
break;
/***********************************************/
/* Format the day of the year (001 - 366). */
/***********************************************/
case 'j' :
len = sprintf(wrkbuf, "%03d", dt -> dt_yday + 1);
if ( len > size ) len = size;
strncpy(dest, wrkbuf, len);
break;
/***********************************************/
/* Format the month of the year (01 - 12). */
/***********************************************/
case 'm' :
len = sprintf(wrkbuf, "%02d", dt -> dt_month + 1);
if ( len > size ) len = size;
strncpy(dest, wrkbuf, len);
break;
/***********************************************/
/* Formats the week of the year. %U starts the */
/* week with Sunday, %W starts the week with a */
/* Monday. */
/***********************************************/
case 'U' :
case 'W' :
start = (*src == 'U') ? 1 : 0;
len = sprintf(wrkbuf, "%02d",
week_of_year(start, dt -> dt_wday, dt->dt_yday));
if ( len > size ) len = size;
strncpy(dest, wrkbuf, len);
break;
/***********************************************/
/* Format the day of the week (1 - 6). */
/***********************************************/
case 'w' :
len = sprintf(wrkbuf, "%d", dt -> dt_wday);
if ( len > size ) len = size;
strncpy( dest, wrkbuf, len );
break;
/***********************************************/
/* Formats a traditional Gregorian date with */
/* single exception: The year is four digits. */
/***********************************************/
case 'x' :
len = sprintf(wrkbuf, "%02d/%02d/%04d",
dt -> dt_month + 1,
dt -> dt_mday,
dt -> dt_year);
if (len > size) len = size;
strncpy(dest, wrkbuf, len);
break;
/***********************************************/